<?php
namespace app\api\controller;

use app\BaseController;
use app\common\model\mysql\CommonUser as models;

class User extends BaseController
{

    public function initialize()
    {
    }

    /*
     * 绑定银行卡
     */
    public function submitBank(){
        $fildes = 'token,bank_name,bank_number,bank_account,remark';
        $receiveData = getData($this->request->post(),$fildes);
        if(empty($receiveData['bank_name'])){
            jsonFail('请输入开户人姓名');
        }
        if(empty($receiveData['bank_number'])){
            jsonFail('请输入银行卡号');
        }
        if(empty($receiveData['bank_account'])){
            jsonFail('请输入开户行信息');
        }
        //校验token
        $admin = models::checkToken($receiveData['token']);
        models::where(['id'=>$admin['id']])->update([
            'bank_name' => $receiveData['bank_name'],
            'bank_number' => $receiveData['bank_number'],
            'bank_account' => $receiveData['bank_account'],
            'remark' => $receiveData['remark'],
            'is_valid' => 1
        ]);
        jsonSuccess();
    }

    /*
     * 更改头像
     */
    public function updateHeadPic(){
        $fildes = 'token,head_pic';
        $receiveData = getData($this->request->post(),$fildes);
        $customer = models::checkToken($receiveData['token']);
        models::where(['id'=>$customer['id']])->update([
            'head_pic' => getImage($receiveData['head_pic']),
        ]);
        jsonSuccess();
    }

    /*
     * 加载登录人信息
     */
    public function load(){
        $fildes = 'token';
        $receiveData = getData($this->request->post(),$fildes);
        $customer = models::checkToken($receiveData['token']);
        $data = [
            'username' => $customer['username'],
            'price' => $customer['price'],
            'bank_name' => $customer['bank_name'],
            'bank_number' => $customer['bank_number'],
            'bank_account' => $customer['bank_account'],
            'bank_mobile' => $customer['bank_mobile'],
            'id_number' => $customer['id_number'],
            'is_valid' => $customer['is_valid'],
            'id' => $customer['id'],
            'head_pic' => fullurl($customer['head_pic']),
            'id_number_d' => $customer['id_number'],
            'bank_number_d' => encryptionStr($customer['bank_number']),
        ];
        jsonSuccess($data);
    }

    /*
     * 修改提现密码
     */
    public function updatePresentPassword(){
        $fildes = 'token,old_present_pwd,present_pwd';
        $receiveData = getData($this->request->post(),$fildes);
        $customer = models::checkToken($receiveData['token']);
        if(!empty($customer['present_pwd']) && pwdMd5($receiveData['old_present_pwd']) != $customer['present_pwd']){
            jsonFail('原提现密码不正确');
        }
        if(empty($receiveData['present_pwd'])){
            jsonFail('请输入新提现密码');
        }
        if($receiveData['old_present_pwd'] == $receiveData['present_pwd']){
            jsonFail('新提现密码不可与原提现密码相同');
        }
        if(strlen($receiveData['present_pwd']) < 6){
            jsonFail('新提现密码不可少于6位');
        }
        models::where(['id'=>$customer['id']])->update([
            'present_pwd' => pwdMd5($receiveData['present_pwd']),
        ]);
        jsonSuccess();
    }

}
